var base_url_indexBrand = '/rs/brand';
var base_url_prc2 = '/rs/prc2';
var base_url_citys = '/rs/openCity';
var base_url_city = '/rs/city';

var list = [];
var categories = [];
var citys = [];
var operation = "add";
var currentPageNo = 1;
var pageRows = 10;

$(function() {
    authName = getCookie('username');
    userCity = getCookie('usercity');
    userCategory = getCookie('usercategory');

    if(userCategory == 9) {
        queryCitys();
    }
    else if(userCategory == 8){
        setCity();
    }

    $("#btnUserQuery").click(function() {
        currentPageNo = 1;
        if(userCategory == 9) {
            queryListAll();
        }
        else if(userCategory == 8){
            queryList();
        }
    });
});

function setCity() {
    zhget(base_url_city, {
        id:userCity
    }, function(result) {
        var cityList = $("#city_id");
        citys = result.rows;
        var text = result.rows[0].name;
        var value = userCity;
        cityList.append("<option value='" + value + "'>" + text + "</option>");
        queryCategories();
    });
}

function queryCitys() {
    zhget(base_url_citys, {
        sort:'orderid'
    }, function(result) {
        var cityList = $("#city_id");
        citys = result.rows;
        for (var al in citys) {
            var text = citys[al].name;
            var value = citys[al].id;
            cityList.append("<option value='" + value + "'>" + text + "</option>");
        }
        queryCategories();
    });
}


function categoryChg(){
    if(userCategory == 9) {
        queryListAll();
    }
    else if(userCategory == 8){
        queryList();
    }
}

function queryCategories() {
    zhget(base_url_prc2, {
        pid:0
    }, function(result) {
        var categoryList = $("#tisid");
        var categoryList2 = $("#type_id");
        categories = result.rows;
        for (var al in categories) {
            var text = categories[al].prcname1;
            var value = categories[al].id;
            categoryList.append("<option value='" + value + "'>" + text + "</option>");
            categoryList2.append("<option value='" + value + "'>" + text + "</option>");
        }
        if(userCategory == 9) {
            queryListAll();
        }
        else if(userCategory == 8){
            queryList();
        }
    });
}

function queryListAll() {
    zhget(base_url_indexBrand, {
        type_id: $('#tisid').val(),
        page: currentPageNo,
        size: pageRows,
        sort: 'orderid'
    }, function(result) {
        list = result.rows;
        for(var i = 0; i<list.length; i++) {
            for(var j = 0 ; j < citys.length ; j++){
                if(result.rows[i]['city_id']==citys[j]["id"]){
                    result.rows[i]['city_name'] = citys[j]['name'];
                    break;
                }
            }
        }
        for(var i = 0; i<list.length; i++) {
            for(var j = 0 ; j < categories.length ; j++){
                if(result.rows[i]['type_id']==categories[j]["id"]){
                    result.rows[i]['category'] = categories[j]['prcname1'];
                }
             }
        }
        buildTable(result, 'indexBrand-template', 'indexBrand-placeholder');
    });
}

function queryList() {
    zhget(base_url_indexBrand, {
        type_id: $('#tisid').val(),
        city_id:userCity,
        page: currentPageNo,
        size: pageRows,
        sort: 'orderid'
    }, function(result) {
        list = result.rows;
        for(var i = 0; i<list.length; i++) {
            for(var j = 0 ; j < citys.length ; j++){
                if(result.rows[i]['city_id']==citys[j]["id"]){
                    result.rows[i]['city_name'] = citys[j]['name'];
                    break;
                }
            }
        }
        for(var i = 0; i<list.length; i++) {
            for(var j = 0 ; j < categories.length ; j++){
                if(result.rows[i]['type_id']==categories[j]["id"]){
                    result.rows[i]['category'] = categories[j]['prcname1'];
                }
            }
        }
        buildTable(result, 'indexBrand-template', 'indexBrand-placeholder');
    });
}

function onAddClick() {
    cleanForm();
    operation = "add";
    $('#userModal').modal('show');
}

function onUpdateClick(id) {
    fillForm(id);
    operation = "modify";
    $('#userModal').modal('show');
}

function onDeleteClick(id) {
    if(confirm("确认要删除？")) {
        zhdelete(base_url_indexBrand + "/" + id, {
            AUTHNAME:authName
        }, function (result) {
            if (result && result.affectedRows > 0) {
                if(userCategory == 9) {
                    queryListAll();
                }
                else if(userCategory == 8){
                    queryList();
                }
                showSuccess('删除成功！');
            } else {
                showError('删除失败！');
            }
        });
    }
}

function onSaveClick() {
    var id = $("#id").val();
    var data = {
        AUTHNAME:authName,
        name: $("#name").val(),
        type_id: $("#type_id").val(),
        picpath: $("#picpath").val(),
        url: $("#url").val(),
        city_id: $("#city_id").val(),
        orderid: $("#orderid").val()
    };
    if (operation == "add") {
        zhpost(base_url_indexBrand, data, saveResult);
    } else {
        zhput(base_url_indexBrand + "/" + id, data, saveResult);
    }
}

function saveResult(result) {
    if (result) {
        if (result.outcome==0) {
            showError('保存失败！');
        } else{
            $('#userModal').modal('hide');
            if(userCategory == 9) {
                queryListAll();
            }
            else if(userCategory == 8){
                queryList();
            }
            showSuccess('保存成功！');
        }
    } else {
        showError('保存失败！');
    }
}

function fillForm(id) {
    var index = 0; 
    for (index in list) {
        var item = list[index];
        if (item.id == id) {
            $("#id").val(item.id);
            $("#name").val(item.name);
            for(var i=0; i<categories.length; i++){
                if(categories[i]['prcname1']===item.category) {
                    $("#type_id").val(i+1501);
                    break;
                }
            }
            $("#picpath").val(item.picpath);
            $("#url").val(item.url);
            $("#orderid").val(item.orderid);
            $("#city_id").val(item.city_id);
            return;
        }
    }
}

function cleanForm() {
    $("#id").val("");
    $("#name").val("");
    $("#type_id").val($("#tisid").val());
    $("#picpath").val("");
    $("#url").val("");
    $("#orderid").val("");
}
